package com.ilia.leek.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ilia.leek.common.enums.ResultCode;
import com.ilia.leek.common.result.ResultResponse;
import com.ilia.leek.entity.Fund;
import com.ilia.leek.entity.FundCompany;
import com.ilia.leek.entity.pojo.QueryFund;
import com.ilia.leek.fund.FundConstant;
import com.ilia.leek.service.FundCompanyService;
import com.ilia.leek.service.FundService;
import com.ilia.leek.service.UserFundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alice on 2021/4/23
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("fund")
@Api(tags = "基金模块")
@ApiSupport(author = "ilia")
public class FundController {

    private final FundService fundService;
    private final UserFundService userFundService;
    private final FundCompanyService fundCompanyService;

    public FundController(FundService fundService, UserFundService userFundService, FundCompanyService fundCompanyService) {
        this.fundService = fundService;
        this.userFundService = userFundService;
        this.fundCompanyService = fundCompanyService;
    }

    /**
     * 添加我的基金
     *
     * @param queryFund 查询基金
     * @return {@link ResultResponse<Object>}
     */
    @PostMapping("addMyFund")
    @ApiOperation(value = "添加我的基金")
    @ApiOperationSupport(
            includeParameters = {
                    "queryFund.userId", "queryFund.favorite",
                    "queryFund.order", "queryFund.fundCode"},
            ignoreParameters = {
                    "fundName", "pinyin",
                    "fundType", "companyCode", "companyName",
                    "page", "pageSize"})
    public ResultResponse<Object> addMyFund(@RequestBody QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund, queryFund.getFundCode())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userFundService.addMyFund(queryFund);
    }


    /**
     * 解绑基金
     *
     * @param queryFund 查询基金
     * @return {@link ResultResponse<Object>}
     */
    @PostMapping("unlockMyFund")
    @ApiOperation(value = "解绑基金")
    @ApiOperationSupport(
            includeParameters = {
                    "queryFund.userId", "queryFund.fundCode"})
    public ResultResponse<Object> unlockMyFund(@RequestBody QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund, queryFund.getFundCode())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userFundService.unlockMyFund(queryFund);
    }

    /**
     * 最喜欢的基金
     *
     * @param queryFund 查询基金
     * @return ResultResponse
     */
    @PostMapping("favoriteFund")
    @ApiOperation(value = "收藏基金")
    @ApiOperationSupport(
            includeParameters = {
                    "queryFund.userId", "queryFund.fundCode"})
    public ResultResponse<Object> favoriteFund(@RequestBody QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund, queryFund.getFundCode())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userFundService.favoriteFund(queryFund);
    }

    /**
     * 搜索基金
     *
     * @param queryFund 查询基金
     * @return {@link ResultResponse<Object>}
     */
    @GetMapping("searchFund")
    @ApiOperation(value = "搜索基金")
    public ResultResponse<Object> searchFund(QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return fundService.searchFunds(queryFund);
    }

    /**
     * 分页查询基金
     *
     * @param queryFund 查询基金
     * @return {@link ResultResponse<Object>}
     */
    @GetMapping("listFund")
    @ApiOperation(value = "分页查询基金")
    public ResultResponse<Object> listFund(QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userFundService.getMyFunds(queryFund);
    }

    /**
     * 查询基金实时信息
     *
     * @param queryFund 查询基金
     * @return {@link ResultResponse<Object>}
     */
    @GetMapping("realTimeFundByCode")
    @ApiOperation(value = "查询基金实时信息")
    @ApiOperationSupport(
            includeParameters = {
                    "queryFund.userId", "queryFund.fundCode"})
    public ResultResponse<Object> realTimeFundByCode(QueryFund queryFund) {
        if (ObjectUtil.hasEmpty(queryFund, queryFund.getFundCode())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        if (queryFund.getFundCode().length() != FundConstant.FUND_CODE_LENGTH) {
            return ResultResponse.failed(ResultCode.PARAMETER_ERROR);
        }
        return fundService.realTimeFundByCode(queryFund.getFundCode());
    }

    /**
     * 写入所有基金数据到基金表
     * 一次写入 受益终身
     * 请求列表:
     * <p>
     * http://fund.eastmoney.com/js/fundcode_search.js
     * </p>
     */
    @Deprecated
    @GetMapping("fundFlush")
    public ResultResponse<Object> pullAll() {
        FileReader fileReader = new FileReader("fund_list.json");
        String result = fileReader.readString();
        JSONArray array = JSONUtil.parseArray(result);
        List<Fund> list = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONArray jsonArray = array.getJSONArray(i);
            Fund fund = new Fund();
            fund.setFundCode(jsonArray.getStr(0));
            fund.setShortName(jsonArray.getStr(1));
            fund.setFundName(jsonArray.getStr(2));
            fund.setFundType(jsonArray.getStr(3));
            fund.setFundFullPinyin(jsonArray.getStr(4));
            list.add(fund);
        }
//        fundService.saveBatch(list);
        List<Fund> funds = fundService.list();
        List<Fund> n = list.stream().filter(item -> !funds.contains(item)).collect(Collectors.toList());
        fundService.saveBatch(list);
        return ResultResponse.success();
    }

    /**
     * 写入所有基金公司数据到基金公司表
     * 一次写入 受益终身
     */
    @Deprecated
    public ResultResponse<Object> pullAllCompany() {
        FileReader fileReader = new FileReader("company.json");
        String result = fileReader.readString();
        JSONArray array = JSONUtil.parseArray(result);
        List<FundCompany> list = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONArray jsonArray = array.getJSONArray(i);
            FundCompany company = new FundCompany();
            company.setCompanyCode(jsonArray.getStr(0));
            company.setCompanyName(jsonArray.getStr(1));
            list.add(company);
        }
//        fundCompanyService.saveBatch(list);
        List<FundCompany> companies = fundCompanyService.list();
        List<FundCompany> n = list.stream().filter(item -> !companies.contains(item)).collect(Collectors.toList());
        for (FundCompany company : n) {
            fundCompanyService.save(company);
        }
        return ResultResponse.success();
    }
}
